/*
 * @Description: 赋权上下文
 * @Author: Rfan
 * @Date: 2022-03-29 13:57:08
 * @LastEditTime: 2023-02-06 10:59:10
 */

import { createContext, useContext, useReducer } from 'react';
import { RightApproveReducer } from '../reducers/RightApproveReducer';
import {
  HIDE_APPROVE_DIALOG,
  SET_APPROVE_LIST,
  SET_QUERY,
  SHOW_APPROVE_DIALOG,
  SUBMIT_APPROVE,
  SET_APPROVE_NAV
} from '../constants';
import { EnumApproveNavTypes } from '@/view/use/RightApprove/dictionaries';
import { fetchRightsProcessDetail } from '@/api/workFlow/workflow';
import {
  fetchProcessDetailByPage,
  completeProcessDetail,
  processDetailComplete
} from '@/api/workFlow/task';
const initialState = {
  // 借阅管理列表
  borrowList: [],
  // 利用审批列表
  approveList: [],
  approveLoading: false,
  approveQuery: {
    pageNo: 1,
    pageSize: 10,
    param: {}
  },
  approveNav: EnumApproveNavTypes.Undo,
  // 选中的表格条目
  selectItems: [],

  approveDialog: {
    visible: false,
    data: null
  }
};

const RightApproveContext = createContext();

const RightApproveContextProvider = (props) => {
  const [state, dispatch] = useReducer(RightApproveReducer, initialState);

  const actions = {
    setApproveNav(navKey) {
      dispatch({ type: SET_APPROVE_NAV, navKey });
    },
    /**
     * @description: 获取审核任务列表
     * @param {string} assignee
     */
    getApproveList(query) {
      return new Promise((resolve, reject) => {
        fetchProcessDetailByPage(query)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_APPROVE_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    /**
     * @description: 获取审核任务列表
     * @param {any} query
     */
    getApproveCompleteList(query) {
      return new Promise((resolve, reject) => {
        processDetailComplete(query)
          .then((res) => {
            const { data } = res;
            dispatch({ type: SET_APPROVE_LIST, data: data.results });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    setListQuery(info) {
      dispatch({ type: SET_QUERY, info });
    },

    /**
     * @description: 打开查看申请弹窗
     */
    showApproveDialog(info) {
      fetchRightsProcessDetail(info.applyForm.id).then((res) => {
        const { data } = res;
        const dialogType = info.status === 0 ? 'approve' : 'view';
        data.taskId = info.taskId;
        dispatch({ type: SHOW_APPROVE_DIALOG, data, dialogType });
      });
    },

    /**
     * @description: 打开查看申请弹窗
     */
    hideApproveDialog(data) {
      dispatch({ type: HIDE_APPROVE_DIALOG, data });
    },

    /**
     * @description:
     * @param {*} params
     * @return {*}
     */
    submitApprove(params) {
      completeProcessDetail(params).then((res) => {
        // const { data } = res;
        dispatch({ type: SUBMIT_APPROVE });
      });
    }
  };

  // // watch query 加载列表
  // useEffect(() => {
  //   console.log('watch');
  //   if (state.approveQuery?.param?.assignee) {
  //     actions.getApproveList();
  //   }
  // }, [state.approveQuery]);

  return (
    <RightApproveContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </RightApproveContext.Provider>
  );
};

export const useRightApproveContext = () => {
  return useContext(RightApproveContext);
};

export { RightApproveContext, RightApproveContextProvider };
